package sky.wicket;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.tro.AirportFinder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 22.09.13
 * Time: 13:54
 * To change this template use File | Settings | File Templates.
 */
public class AirportFilter extends BasePage {

    private AirportFinder airportFinder = new AirportFinder();
    static final Logger log = LoggerFactory.getLogger(AddUpdateAirport.class);

    public AirportFilter() {
        addElementsToPage();
    }

    private void addElementsToPage() {
        add(new FeedbackPanel("feedback"));
        add(new Label("criteriaLabel", "Criteia: "));
        Form<AirportFinder> airportCriteria = new Form<AirportFinder>("airportCriteria", new CompoundPropertyModel<AirportFinder>(airportFinder)){
            @Override
            protected void onSubmit() {
                log.info("country: " + airportFinder.getCountryName());
                log.info("city: " + airportFinder.getCityName());
                log.info("code: " + airportFinder.getCode());
                setResponsePage(new AirportList(airportFinder));
            }

            @Override
            protected void onError() {}
        };
        airportCriteria.add(new Label("countryNameLabel", "Country's name: "));
        airportCriteria.add(new Label("cityNameLabel", "City's name: "));
        airportCriteria.add(new Label("codeLabel", "Code: "));
        airportCriteria.add(new TextField<String>("countryName"));
        airportCriteria.add(new TextField<String>("cityName"));
        ArrayList<String> listCodes = (ArrayList<String>) airportService.listCodes();
        listCodes.add(0, "");
        DropDownChoice<String> codeChoices = new DropDownChoice<String>("code", listCodes);
        airportCriteria.add(codeChoices);
        add(airportCriteria);
    }
}
